from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys
import threading
import time

from brief_info import brief_info
from disk_usage import get_diskusage
from mem_info import get_meminfo
import proc_info_view

def async_call(fn):
    def wrapper(*args, **kwargs):
        threading.Thread(target=fn, args=args, kwargs=kwargs).start()
    return wrapper

class Ui_SysInfo(QDialog):
    def __init__(self):
        super().__init__()
        self.setFixedSize(900, 800)
        self.setWindowTitle("系统资源监控")
        
        self.label_brief = QtWidgets.QLabel()
        self.label_disk = QtWidgets.QLabel()
        self.label_mem = QtWidgets.QLabel()
        self.label_client = QtWidgets.QLabel()
        self.label_server = QtWidgets.QLabel()
        self.label_brief.setText("系统概略")
        self.label_disk.setText("磁盘使用")
        self.label_mem.setText("内存使用")
        self.label_client.setText("客户端进程详情")
        
        self.textBrowser_brief = QtWidgets.QTextBrowser()
        self.textBrowser_disk = QtWidgets.QTextBrowser()
        self.textBrowser_mem = QtWidgets.QTextBrowser()
        
        self.btn_client= QtWidgets.QPushButton("打开客户端详情")
        self.btn_server= QtWidgets.QPushButton("打开服务端详情")

        self.connect()
        self.layout_ui()
        
        self.update_timer = QTimer() 
        self.update_timer.start(1000)  
        self.update_timer.timeout.connect(self.update_view)
        self.briefinfo = ""
        self.diskusage = ""
        self.meminfo = ""
    
    def layout_ui(self):
        self.h_brief = QHBoxLayout()
        self.h_brief.addWidget(self.label_brief)
        self.h_brief.addWidget(self.textBrowser_brief)
        
        self.h_disk = QHBoxLayout()
        self.h_disk.addWidget(self.label_disk)
        self.h_disk.addWidget(self.textBrowser_disk)
        
        self.h_mem = QHBoxLayout()
        self.h_mem.addWidget(self.label_mem)
        self.h_mem.addWidget(self.textBrowser_mem)
        
        self.h_btn = QHBoxLayout()
        self.h_btn.addWidget(self.btn_client)
        self.h_btn.addWidget(self.btn_server)
        
        self.v_box = QVBoxLayout()
        self.v_box.addLayout(self.h_brief)
        self.v_box.addLayout(self.h_disk)
        self.v_box.addLayout(self.h_mem)
        self.v_box.addLayout(self.h_btn)
        self.setLayout(self.v_box)
    
    def showClientProcView(self):
        dlg = proc_info_view.Ui_Process("Heduian.exe")
        dlg.exec()
        
    def showServerProcView(self):
        dlg = proc_info_view.Ui_Process("SunloginRemote.exe")
        dlg.exec()
        
    def connect(self):
        self.btn_client.clicked.connect(self.showClientProcView)
        self.btn_server.clicked.connect(self.showServerProcView)
        
    @async_call
    def getinfo(self):
        self.briefinfo = brief_info()
        self.diskusage = get_diskusage()
        self.meminfo = get_meminfo()
    
    def update_view(self):
        self.getinfo()
        self.textBrowser_brief.setText(self.briefinfo)
        self.textBrowser_disk.setText(self.diskusage)
        self.textBrowser_mem.setText(self.meminfo)

        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Ui_SysInfo()
    ui.setFixedSize(800, 600)
    ui.show()
    sys.exit(app.exec_())